<?php
namespace app\common\logic;

use app\common\component\user\UserModel;
use app\common\component\user_wallet\UserWalletService;

class UserWallet {
	
	/*
	 * 发放奖励
	 */
	public function doReward($user_id, $money) {
		$resultUser = UserModel::get(['user_id' => $user_id]);

		if($resultUser -> getData('promotion_id') == 0) {
			return ['code' => 0, 'message' => '没有推荐人'];
		}

		$layers = json_decode(getParam('USER_REWARD_LAYERS_JSON'), true);

		$resultPromotionUser = UserModel::get(['user_id' => $resultUser -> getData('promotion_id')]);
		for($i = 1;$i <= getParam('USER_REWARD_LAYERS');$i++) {
			if(isset($layers[$i]) && is_numeric($layers[$i])) {
				(new UserWalletFreeze) -> doFreeze($resultPromotionUser -> user_id, $layers[$i] / 100 * $money, false, '冻结奖金');

			} else {
				return ['code' => 0, 'message' => '层数错误'];
			}

			if($resultPromotionUser -> getData('promotion_id') == 0) {
				return ['code' => 0, 'message' => '没有推荐人'];
			}

			$resultPromotionUser = UserModel::get(['user_id' => $resultPromotionUser -> getData('promotion_id')]);
		}

		return ['code' => 0, 'message' => '奖金发放完成!'];
	}

	/*
	 * 设置提现额度
	 */
	public function withdrawPosition($user_id, $money) {
		$getUserWallet = (new UserWalletService()) -> getUserWallet($user_id);
		$getUserWallet['data']['user_wallet'] -> withdraw_position += $money * getParam('WITHDRAW_DOUBLE');
		if(!$getUserWallet['data']['user_wallet'] -> save()) {
			return ['code' => -1, 'message' => '提现操作失败!'];
		}
		return ['code' => 0, 'message' => '提现操作成功!'];
	}
}
?>